<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\validate\AddorderValidate;
use app\shop\model\AddOrderModel;
use app\shop\model\PurchaseModel;
use app\common\model\Category;
use app\common\model\Shop;
use app\common\model\User;
use think\Exception;
use think\Request;
use think\Session;

/**
* 订单管理
*/
class Addorder extends Common
{
	/**
	* 新建售货单
	*/
	public function addorder()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
                /*参数验证*/
				 $AddorderValidate = new AddorderValidate();
				 if(!$AddorderValidate->scene('addorder')->check($info))
				 {
					return json(back(config('status.STATUS_CODE_SIX'),$AddorderValidate->getError()));
				 }
				return json(AddOrderModel::addOrder($info));
            }catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('ordersn',self::ordersn());
        $this->assign('shopname',(new Shop())->shopField(['shop_id'=>Session::get('admin.shop_id')],'shop_name'));
		return $this->fetch();
	}
	/**
	* 查询客户
	*/
	public function userlist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return (new User())->getUserList(10,$info['page']);
		}
		return $this->fetch();
	}
    /**
    * 用户详情
    */
    public function userinfo()
    {
        $info = Request::instance()->param();
        $userinfo = User::get($info['id']);
        $userinfo['u_type'] = $userinfo['u_type']==0?'新客':'老客';
        switch($userinfo['sex'])
        {
            case 0:$userinfo['sex']='保密';break;
            case 1:$userinfo['sex']='男';break;
            case 2:$userinfo['sex']='女';break;
        }
        $this->assign('userinfo',$userinfo);
        return $this->fetch();
    }
    /**
    * 搜索用户信息
    */
    public function eachuser()
    {
        try{
            $info = Request::instance()->param();
            return json(AddOrderModel::getUserList($info));
        }catch (\Exception $e){
            return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
        }
    }
    /**
    * 选择商品列表
    */
    public function ogoodslist()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
			return AddOrderModel::getGoodsList($info);
		}
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
       return $this->fetch(); 
    }
    /**
    * 新建采购单
    */
    public function setpurchase()
    {
        try{
            $info = Request::instance()->param();
            return json(AddOrderModel::purchaseList($info['id']));
        }catch (\Exception $e){
            return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
        }
    }
    /**
    * 搜索某一个商品信息
    */
    public function ogoodsinfo()
    {
        try{
            $info = Request::instance()->param();
            return json(AddOrderModel::ogoodsInfo($info));
        }catch (\Exception $e){
            return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
        }
    }
    /**
    * 获取活动信息
    */
    public function orderactiv()
    {
        try{
            $info = Request::instance()->param();
            return json(AddOrderModel::orderActivit($info));
        }catch (\Exception $e){
            return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
        }
    }
    /**
    * 计算活动订单价格
    */
    public function countorderpay()
    {
        try{
            $info = Request::instance()->param();
            return AddOrderModel::countOrderPay($info);
        }catch (\Exception $e){
            return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
        }
    }
    /**********订单编号***********/
    /**
    * 订单编号生成
    */
    private function ordersn()
    {
        return 'WX' . time() . rand(10000,99999);
    }
}
